package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.MakeFriendManager;
import com.itheima.domain.mongo.Sound;
import com.itheima.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class MakeFriendController {

    @Autowired
    private MakeFriendManager makeFriendManager;



    //今日佳人
    @GetMapping("/tanhua/todayBest")
    public RecommendUserVo findTodayBest(){
        return makeFriendManager.findTodayBest();
    }

    //推荐用户列表
    @GetMapping("/tanhua/recommendation")
    public PageBeanVo findRecommendUserList( @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                             @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize )
    {
        return makeFriendManager.findRecommendUserList(pageNum,pageSize);
    }

    //推荐用户详细信息(单人)
    @GetMapping("/tanhua/{id}/personalInfo")
    public RecommendUserVo findRecommendUserInfo(@PathVariable("id") Long recommendId){
        return makeFriendManager.findRecommendUserInfo(recommendId);
    }

    //聊一下 -- 查看陌生人问题
    @GetMapping("/tanhua/strangerQuestions")
    public String findStrangerQuestion(Long userId){
        return makeFriendManager.findStrangerQuestion(userId);
    }

    //最近访客(谁看过我)
    @GetMapping("/movements/visitors")
    public List<VisitorVo> findVisitorList(){
        return makeFriendManager.findVisitorList();
    }

    //上报地理位置
    @PostMapping("/baidu/location")
    public void saveUserLocation(@RequestBody Map<String,String> map){
        //纬度
        Double latitude = Double.parseDouble(map.get("latitude"));
        //经度
        Double longitude = Double.parseDouble(map.get("longitude"));
        //位置描述(地址名)
        String addrStr = map.get("addrStr");
        System.out.println(longitude + "===========" + latitude);
        makeFriendManager.saveUserLocation(latitude,longitude,addrStr);
    }

    //搜索附近的人
    @GetMapping("/tanhua/search")
    public List<NearUserVo> findNearUserList(String gender,Long distance){
        return makeFriendManager.findNearUserList(gender,distance);
    }

    //获取环信用户信息
    @GetMapping("/huanxin/user")
    public Map<String, String> huanxinUser() {

        Long userId = UserHolder.get().getId();

        Map<String, String> result = new HashMap<>();

        result.put("username",userId.toString());
        result.put("password","123456");

        return result;
    }

    //回复陌生人问题
    @PostMapping("/tanhua/strangerQuestions")
    public void strangerQuestions(@RequestBody Map map){
        //这里的userId是佳人id，也就是你要和谁聊一下，她的用户id
        Integer strangeId = (Integer) map.get("userId");
        String reply = (String) map.get("reply");

        //调用manager打招呼
        makeFriendManager.replyStrangerQuestion(strangeId,reply);
    }

    //添加好友(聊一下)
    @PostMapping("/messages/contacts")
    public void saveFriend(@RequestBody Map<String,Integer> map){
        //好友id
        long friendId = map.get("userId").longValue();
        makeFriendManager.saveFriend(friendId);
    }

    //查询我的好友
    @GetMapping("/messages/contacts")
    public PageBeanVo findMyFriend( @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                    @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize)
    {
        return makeFriendManager.findMyFriend(pageNum,pageSize);
    }

    //探花-好友卡片展示
    @GetMapping("/tanhua/cards")
    public List<RecommendUserVo> findCards(){
        //只要前二十条推荐数据
        return (List<RecommendUserVo>) makeFriendManager.findRecommendUserList(1,20).getItems();
    }

    //桃花传音-接收语音（学生实战）
    @GetMapping("/peachblossom")
    public SoundVo getVoice(){
      return   makeFriendManager.getVoice();
    }

    //桃花传音-发送语音（学生实战）
    @PostMapping("/peachblossom")
    public void setVoice(MultipartFile soundFile) throws IOException {
        makeFriendManager.setVoice(soundFile);
    }
}
